package com.api.demo.test.business;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.api.demo.dto.business.invoice.ApplicationInvoiceDTO;
import com.api.demo.dto.business.invoice.ApplicationInvoiceV2DTO;
import com.api.demo.dto.business.invoice.BillingCategoryDTO;
import com.api.demo.util.ApiSignUtil;
import com.api.demo.util.CommonUtil;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 发票相关
 * @author: yanming
 * @createDate: 2022/12/26 14:28
 */
public class Invoice {

    /**
     * 查询开票修要的参数v2
     */
    public static String getInvoiceNeedInfo = "%s/api/invoice/application-invoice-need-info-v2";


    /**
     * 查询申请开票详情v2
     */
    public static String getApplicationInvoiceInfo = "%s/api/invoice/application-invoice-info-v2";

    /**
     * 申请开票
     */
    public static String applicationInvoice = "%s/api/invoice/application-invoice";
//    public static String applicationInvoiceV2 = "%s/api/invoice/application-invoice";
//    public static String applicationInvoiceV3 = "%s/api/invoice/application-invoice";


    /**
     * 查询发票详情v2
     */
    public static String getInvoiceInfo = "%s/api/invoice/invoice-info-v2";

    /**
     * 查询地址IDv2
     */
    public static String getAddress = "%s/api/invoice/address-id-v2";


    public static void main(String[] args) {

        /**
         * 查询开票修要的参数
         */
//        getInvoiceNeedInfo();


        /**
         * 查询申请发票详情
         */
//        getApplicationInvoiceInfo();

        /**
         * 申请开票
         */
//        applicationInvoice();

        /**
         * 申请开票-v2
         */
        applicationInvoiceV2();

        /**
         * 申请开票-v3 (加密字段-全量加密)
         */
//        applicationInvoiceV3();


        /**
         * 查询发票状态信息
         */
//        getInvoiceInfo();

        /**
         * 查询地址
         */
//        getAddressId();


    }


    /**
     * 查询开票修要的参数  （推荐适使用）
     */
    public static void getInvoiceNeedInfo() {
        BillingCategoryDTO billingCategoryDTO = new BillingCategoryDTO();
        billingCategoryDTO.setAgentId(CommonUtil.AGENT_ID);
        billingCategoryDTO.setTimestamp(System.currentTimeMillis());
        billingCategoryDTO.setId(7161881833707352064L);

        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(3);
        signMap.put("agentId", billingCategoryDTO.getAgentId());
        signMap.put("timestamp", billingCategoryDTO.getTimestamp());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        billingCategoryDTO.setSign(sign);

        //请求地址
        String url = String.format(getInvoiceNeedInfo, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(billingCategoryDTO));
        System.out.println(post);

    }

    /**
     * 查询申请开票详情
     */
    public static void getApplicationInvoiceInfo() {

        BillingCategoryDTO billingCategoryDTO = new BillingCategoryDTO();
        billingCategoryDTO.setAgentId(CommonUtil.AGENT_ID);
        billingCategoryDTO.setTimestamp(System.currentTimeMillis());
        billingCategoryDTO.setId(7161881833707352064L);

        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(3);
        signMap.put("agentId", billingCategoryDTO.getAgentId());
        signMap.put("timestamp", billingCategoryDTO.getTimestamp());
        signMap.put("id", billingCategoryDTO.getId());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        billingCategoryDTO.setSign(sign);

        //请求地址
        String url = String.format(getApplicationInvoiceInfo, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(billingCategoryDTO));
        System.out.println(post);


    }


    /**
     * 申请开票
     */
    public static void applicationInvoice() {
        ApplicationInvoiceDTO applicationInvoiceDTO = new ApplicationInvoiceDTO();
        applicationInvoiceDTO.setAgentId(CommonUtil.AGENT_ID);
        applicationInvoiceDTO.setTimestamp(System.currentTimeMillis());
        applicationInvoiceDTO.setId("7161881833707352064");
        applicationInvoiceDTO.setAddressId(1762385853042135042L);
        applicationInvoiceDTO.setInvoiceCatalogId(1760953673736785921L);
        //发票类型 ，SPECIAL（专票） ， COMMON （普票）
        applicationInvoiceDTO.setInvoiceType("SPECIAL");
        //发票方式 ， PAPERY（纸质） ， ELECTRON（电子票)
        applicationInvoiceDTO.setInvoiceWay("ELECTRON");

        //非必填参数
        applicationInvoiceDTO.setApplicationDesc("测试开票demo");
        applicationInvoiceDTO.setEmail("3906448@qq.com");
        applicationInvoiceDTO.setNotifyUrl("www.baidu.com");
        //该预留字段，暂时不要传
//        applicationInvoiceDTO.setBillingAmount(new BigDecimal("1"));
        //--------


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(4);
        signMap.put("agentId", applicationInvoiceDTO.getAgentId());
        signMap.put("timestamp", applicationInvoiceDTO.getTimestamp());
        signMap.put("id", applicationInvoiceDTO.getId());
        signMap.put("invoiceCatalogId", applicationInvoiceDTO.getInvoiceCatalogId());
        signMap.put("key", CommonUtil.KEY);

        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        applicationInvoiceDTO.setSign(sign);

        //请求地址
        String url = String.format(applicationInvoice, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(applicationInvoiceDTO));
        System.out.println(post);

    }

    /**
     * 申请开票-v2
     * (如果有回调的话，回调是按订单回调，多个订单多次回调)
     */
    public static void applicationInvoiceV2() {
        ApplicationInvoiceV2DTO applicationInvoiceDTO = new ApplicationInvoiceV2DTO();
        applicationInvoiceDTO.setAgentId(CommonUtil.AGENT_ID);
        applicationInvoiceDTO.setTimestamp(System.currentTimeMillis());
        applicationInvoiceDTO.setId("7161909609898713088,7161909961851179008");
        applicationInvoiceDTO.setInvoiceCatalogName("*现代服务*企业形象策划");
//        applicationInvoiceDTO.setInvoiceCatalogId(1760953673736785921L);
        //发票类型 ，SPECIAL（专票） ， COMMON （普票）
        applicationInvoiceDTO.setInvoiceType("SPECIAL");
        //发票方式 ， PAPERY（纸质） ， ELECTRON（电子票)
        applicationInvoiceDTO.setInvoiceWay("ELECTRON");

        //地址信息  二选一，必填一项
        //1
//        applicationInvoiceDTO.setAddressId(1762385853042135042L); //使用地址id
        //2
        ApplicationInvoiceV2DTO.AddressVo addressVo = new ApplicationInvoiceV2DTO.AddressVo();
        addressVo.setName("张三");  //必填
        addressVo.setMobile("15099990637"); //必填
        addressVo.setAddress("北京"); //必填
        addressVo.setAddressDesc("北京市丰台区·····"); //必填
        applicationInvoiceDTO.setAddressVo(addressVo); //使用地址信息


        //非必填参数
        applicationInvoiceDTO.setApplicationDesc("测试开票demo");
        applicationInvoiceDTO.setEmail("3906448@qq.com");
        applicationInvoiceDTO.setNotifyUrl("www.baidu.com");
        //该预留字段，暂时不要传
//        applicationInvoiceDTO.setBillingAmount(new BigDecimal("1"));
        //--------

        //全量加密
        Map<String, Object> signMap = JSON.parseObject(JSON.toJSONString(applicationInvoiceDTO), new TypeReference<Map<String, Object>>() {
        });
        signMap.put("key", CommonUtil.KEY);

        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        applicationInvoiceDTO.setSign(sign);

        //请求地址
        String url = String.format(applicationInvoice, CommonUtil.REQUEST_URL) + "-v2";
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(applicationInvoiceDTO));
        System.out.println(post);

    }

    /**
     * 申请开票-v3
     * (如果有回调的话，回调是按发票回调，多个订单也只会回调一次)
     */
    public static void applicationInvoiceV3() {
        ApplicationInvoiceV2DTO applicationInvoiceDTO = new ApplicationInvoiceV2DTO();
        applicationInvoiceDTO.setAgentId(CommonUtil.AGENT_ID);
        applicationInvoiceDTO.setTimestamp(System.currentTimeMillis());
        applicationInvoiceDTO.setId("7161909609898713088,7161909961851179008");
        applicationInvoiceDTO.setInvoiceCatalogName("*现代服务*企业形象策划");
//        applicationInvoiceDTO.setInvoiceCatalogId(1760953673736785921L);
        //发票类型 ，SPECIAL（专票） ， COMMON （普票）
        applicationInvoiceDTO.setInvoiceType("SPECIAL");
        //发票方式 ， PAPERY（纸质） ， ELECTRON（电子票)
        applicationInvoiceDTO.setInvoiceWay("ELECTRON");

        //地址信息  二选一，必填一项
        //1
//        applicationInvoiceDTO.setAddressId(1762385853042135042L); //使用地址id
        //2
        ApplicationInvoiceV2DTO.AddressVo addressVo = new ApplicationInvoiceV2DTO.AddressVo();
        addressVo.setName("张三");  //必填
        addressVo.setMobile("15099990637"); //必填
        addressVo.setAddress("北京"); //必填
        addressVo.setAddressDesc("北京市丰台区·····"); //必填
        applicationInvoiceDTO.setAddressVo(addressVo); //使用地址信息


        //非必填参数
        applicationInvoiceDTO.setApplicationDesc("测试开票demo");
        applicationInvoiceDTO.setEmail("3906448@qq.com");
        applicationInvoiceDTO.setNotifyUrl("www.baidu.com");
        //该预留字段，暂时不要传
//        applicationInvoiceDTO.setBillingAmount(new BigDecimal("1"));
        //--------

        //全量加密
        Map<String, Object> signMap = JSON.parseObject(JSON.toJSONString(applicationInvoiceDTO), new TypeReference<Map<String, Object>>() {
        });
        signMap.put("key", CommonUtil.KEY);

        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        applicationInvoiceDTO.setSign(sign);

        //请求地址
        String url = String.format(applicationInvoice, CommonUtil.REQUEST_URL) + "-v3";
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(applicationInvoiceDTO));
        System.out.println(post);

    }


    /**
     * 查询发票详情
     */
    public static void getInvoiceInfo() {

        BillingCategoryDTO billingCategoryDTO = new BillingCategoryDTO();
        billingCategoryDTO.setAgentId(1515854855753699330L);
        billingCategoryDTO.setTimestamp(System.currentTimeMillis());
        billingCategoryDTO.setId(7007164004633313280L);

        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(3);
        signMap.put("agentId", billingCategoryDTO.getAgentId());
        signMap.put("timestamp", billingCategoryDTO.getTimestamp());
        signMap.put("key", "123");
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        billingCategoryDTO.setSign(sign);

        //请求地址
        String url = String.format(getInvoiceInfo, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(billingCategoryDTO));
        System.out.println(post);
    }

    /**
     * 查询地址ID
     */
    public static void getAddressId() {
        BillingCategoryDTO billingCategoryDTO = new BillingCategoryDTO();
        billingCategoryDTO.setAgentId(1515854855753699330L);
        billingCategoryDTO.setTimestamp(System.currentTimeMillis());
        billingCategoryDTO.setId(7007164004633313280L);

        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(3);
        signMap.put("agentId", billingCategoryDTO.getAgentId());
        signMap.put("timestamp", billingCategoryDTO.getTimestamp());
        signMap.put("key", "123");
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        billingCategoryDTO.setSign(sign);

        //请求地址
        String url = String.format(getAddress, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(billingCategoryDTO));
        System.out.println(post);
    }


}